
import {Inject, Injectable} from '@nestjs/common';
import {REQUEST} from "@nestjs/core";
import {User} from "@/entitys/base/user.entity";
import {PageProps} from "@/typing/paging/paging";

@Injectable()
export class CommonUtils {
    constructor(@Inject(REQUEST) private readonly request: Request) {

    }

    getUser(): User {
        // @ts-ignore
       return this.request.user;
    }

    getPagination(paginationProps: PageProps) {
        const orderBy = paginationProps.orderBy;
        const order = paginationProps.order || "DESC";
        const page = paginationProps.page || 1;
        const pageSize = paginationProps.pageSize || 20;
        const skip = (page - 1) * pageSize;
        const pagination = {
            take: pageSize,
            skip,
            ...(orderBy && {
                [orderBy]: orderBy
            }),
        };
        return pagination;

    }

}
